/**
 * Project Name:gc_sms_mvn
 * File Name:TreeUtil.java
 * Package Name:com.gc.util.Tree
 * Date:2018年11月7日下午1:20:17
 * Copyright (c) 2018,http://www.clpsglobal.com/zh/home-cn/ All Rights Reserved.
 *
 */
package com.clps.dev.sms.util.tree;

import java.util.ArrayList;
import java.util.List;

import com.clps.dev.sms.entity.sm.Menu;

/**
 * 
 * @ClassName: TreeUtil <br/>
 * @Description: <br/>
 *
 * @date 2018-12-19 09:27:27 <br/>
 *
 * @author long.ma
 * @version V1.0
 * @since JDK 1.8
 */
public class TreeUtil {

	/**
	 * 
	 * @description createTreeOfMenu <br/>
	 *              构建树形结构关于Menu
	 * 
	 *
	 * @author long.ma
	 * @param list
	 * @return
	 * @since JDK 1.8
	 */
	public static String createTreeOfMenu(List<Menu> list) {
		List<TreeBean<Menu>> root = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			if (0 == list.get(i).getMenuParentId().intValue()) {
				TreeBean<Menu> node = new TreeBean<>();
				node.setNodeId(String.valueOf(list.get(i).getMenuId()));
				node.setNodeName(list.get(i).getMenuName());
				node.setNodeParentId(String.valueOf(list.get(i).getMenuParentId()));
				node.setNodeIcon(list.get(i).getMenuSymbolPath());
				node.setNodeUrl(list.get(i).getMenuUrl());
				node.setNodeChildren(new ArrayList<TreeBean<Menu>>());
				node.setNodeOnOff(false);
				root.add(node);
				list.remove(i);
				i--;
			}
		}

		TreeUtil treeUtil = new TreeUtil();
		treeUtil.findChildrenOfMenu(root, list);

		String jsonString = treeUtil.spliceJsonOfMenu(root);
		return jsonString;
	}

	/**
	 * 
	 * @description findChildrenOfMenu <br/>
	 *              树形结构中的递归关于Menu的
	 * 
	 *
	 * @author long.ma
	 * @param list
	 * @param node
	 * @since JDK 1.8
	 */
	public void findChildrenOfMenu(List<TreeBean<Menu>> listTreeBean, List<Menu> listMenu) {
		if (null == listTreeBean || listTreeBean.size() == 0 || null == listMenu || 0 == listMenu.size()) {
			return;
		} else {
			for (int i = 0; i < listTreeBean.size(); i++) {
				for (int j = 0; j < listMenu.size(); j++) {
					if (listTreeBean.get(i).getNodeId().equals(String.valueOf(listMenu.get(j).getMenuParentId()))) {
						TreeBean<Menu> node = new TreeBean<>();
						node.setNodeId(String.valueOf(listMenu.get(j).getMenuId()));
						node.setNodeName(listMenu.get(j).getMenuName());
						node.setNodeParentId(String.valueOf(listMenu.get(j).getMenuParentId()));
						node.setNodeIcon(listMenu.get(j).getMenuSymbolPath());
						node.setNodeUrl(listMenu.get(j).getMenuUrl());
						node.setNodeChildren(new ArrayList<TreeBean<Menu>>());
						node.setNodeOnOff(false);
						listTreeBean.get(i).getNodeChildren().add(node);
						listMenu.remove(j);
						j--;
					}
				}
			}
			for (int i = 0; i < listTreeBean.size(); i++) {
				findChildrenOfMenu(listTreeBean.get(i).getNodeChildren(), listMenu);
			}
		}
	}

	/**
	 * 
	 * @description spliceJsonOfMenu <br/>
	 *              拼接json类型数据关于Menu
	 * 
	 * @param list
	 * @return
	 *
	 */
	public String spliceJsonOfMenu(List<TreeBean<Menu>> list) {
		String jsonString = "";
		for (TreeBean<Menu> treeBean : list) {
			jsonString += "{id:'" + treeBean.getNodeId() + "'," +
		"text:'" + treeBean.getNodeName() + "'," + "href:'"
					+ treeBean.getNodeUrl() + "'";
			if (treeBean.getNodeChildren().size() > 0) {
				jsonString += ",nodes:[" + spliceJsonOfMenu(treeBean.getNodeChildren()) + "]";
			}
			jsonString += "},";
		}
		return jsonString.substring(0, jsonString.length() - 1);
	}

	/**
	 * 
	 * @description createTreeOfMenuList <br/>
	 *              构建菜单树型结构 列表显示
	 * 
	 *
	 * 
	 * @param list
	 * @return
	 *
	 */
	public static List<TreeBean<Menu>> createTreeOfMenuList(List<Menu> list) {
		List<TreeBean<Menu>> root = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			if (0 == list.get(i).getMenuParentId().intValue()) {
				TreeBean<Menu> node = new TreeBean<>();
				node.setNodeId(String.valueOf(list.get(i).getMenuId()));
				node.setNodeName(list.get(i).getMenuName());
				node.setNodeParentId(String.valueOf(list.get(i).getMenuParentId()));
				node.setNodeIcon(list.get(i).getMenuSymbolPath());
				node.setNodeUrl(list.get(i).getMenuUrl());
				node.setNodeDescription(list.get(i).getMenuDecription());
				node.setNodeCreatedEmployeeId(String.valueOf(list.get(i).getMenuCreatedEmployeeId()));
				node.setNodeCreatedTime(list.get(i).getMenuCreatedTime());
				node.setNodeChildren(new ArrayList<TreeBean<Menu>>());
				node.setNodeOnOff(false);
				root.add(node);
				list.remove(i);
				i--;
			}
		}

		TreeUtil treeUtil = new TreeUtil();
		treeUtil.findChildrenOfMenuList(root, list);
		List<TreeBean<Menu>> listResult = new ArrayList<>();
		treeUtil.inorderTraversalTree(root, listResult);
		return listResult;
	}

	/**
	 * 
	 * @description findChildrenOfMenuList <br/>
	 *              构建树型结构 寻找孩子 列表
	 * 
	 *
	 * 
	 * @param listTreeBean
	 * @param listMenu
	 *
	 */
	public void findChildrenOfMenuList(List<TreeBean<Menu>> listTreeBean, List<Menu> listMenu) {
		if (null == listTreeBean || listTreeBean.size() == 0 || null == listMenu || 0 == listMenu.size()) {
			return;
		} else {
			for (int i = 0; i < listTreeBean.size(); i++) {
				for (int j = 0; j < listMenu.size(); j++) {
					if (listTreeBean.get(i).getNodeId().equals(String.valueOf(listMenu.get(j).getMenuParentId()))) {
						TreeBean<Menu> node = new TreeBean<>();
						node.setNodeId(String.valueOf(listMenu.get(j).getMenuId()));
						node.setNodeName(listMenu.get(j).getMenuName());
						node.setNodeParentId(String.valueOf(listMenu.get(j).getMenuParentId()));
						node.setNodeIcon(listMenu.get(j).getMenuSymbolPath());
						node.setNodeUrl(listMenu.get(j).getMenuUrl());
						node.setNodeDescription(listMenu.get(j).getMenuDecription());
						node.setNodeCreatedEmployeeId(String.valueOf(listMenu.get(j).getMenuCreatedEmployeeId()));
						node.setNodeCreatedTime(listMenu.get(j).getMenuCreatedTime());
						node.setNodeChildren(new ArrayList<TreeBean<Menu>>());
						node.setNodeOnOff(false);
						listTreeBean.get(i).getNodeChildren().add(node);
						listMenu.remove(j);
						j--;
					}
				}
			}
			for (int i = 0; i < listTreeBean.size(); i++) {
				findChildrenOfMenuList(listTreeBean.get(i).getNodeChildren(), listMenu);
			}
		}
	}

	/**
	 * 
	 * @description inorderTraversalTree <br/>
	 *              中序遍历树型结构
	 * 
	 *
	 * 
	 * @param list
	 * @param listResult
	 *
	 */
	public void inorderTraversalTree(List<TreeBean<Menu>> list, List<TreeBean<Menu>> listResult) {
		if (null != list) {
			for (TreeBean<Menu> treeBean : list) {
				listResult.add(treeBean);
				if (null != treeBean.getNodeChildren() && treeBean.getNodeChildren().size() > 0) {
					inorderTraversalTree(treeBean.getNodeChildren(), listResult);
				}
			}
		}
	}

}
